/******************************************/
/******************************************/
/****  JS Document                     ****/
/****  by Andrew Mahon                 ****/
/****  amahon@gmail.com                ****/
/******************************************/
/******************************************/

(function(tc) {

	tc.fn.y5fetcher = function(options){
		var $y5f = this, _o = {}, _framesLoaded = 0, _newestFrameWIndex = 0
		
		_o = tc.jQ.extend({
			
		},options)
		
		function _initialize(){
			tc.util.log("YeahYeahYeahYeahYeah Fetcher Starting",'info')
			FrameInfoForCellblock ('http://www.yeahyeahyeahyeahyeah.com', _o.cellblockId, 0, 10, 'date', 0, '', _cellblockRecievedHandler)
			_setup_events()
			return $y5f
		}
		
		function _setup_events(){
			tc.events.bind('fetchMoreFrames',_fetchMoreFramesHandler)
			tc.events.bind('checkForNew',_checkForNewHandler)
		}
		
		function _fetchMoreFramesHandler(e,d){
			if(!d.n_frames){
				d.n_frames = 10
			}
			FrameInfoForCellblock ('http://www.yeahyeahyeahyeahyeah.com', _o.cellblockId, _framesLoaded, d.n_frames, 'date', 0, '', _cellblockRecievedHandler)
		}
		
		function _checkForNewHandler(e,d){
			if(_newestFrameWIndex){
				//GetFramesNewerThan('http://www.yeahyeahyeahyeahyeah.com', _o.cellblockId, _newestFrameWIndex, _cellblockRecievedHandler)
			}
		}
		
		function _cellblockRecievedHandler(e){
			if(e.frames.length){
				_framesLoaded += e.frames.length
				for(var i in e.frames){
					if(e.frames[i].wIndex > _newestFrameWIndex){
						_newestFrameWIndex = e.frames[i].wIndex
					}
				}
				tc.events.trigger('cellblockFramesLoaded',e)
			}
		}
		
		return _initialize()
	}
})(tc);